( function ($) {
	QVM.prototype.setWidget = function (widget) {
		this.widget = $($(widget)[0]);
		this.widget.addClass();
	};
	QVM.defaultInternals['PRINT'] = function (args, next) {
		var self = this;
		var message = $('<div>');
		
		for(var i = 0; i < args.length; i++) {
			var p = $('<p>').text(String(args[i])).appendTo(message);
		}
		message.appendTo(this.widget);
		
		var ok = $('<button>')
			.text('OK')
			.appendTo(this.widget)
			.focus()
			.click(function () {
				self.widget.empty();
				next();
			});
	};

	QVM.defaultInternals['INPUT'] = function (args, next) {
		var self = this;
		var message = $('<div>');
		var input = $('<textarea>');
		
		for(var i = 0; i < args.length; i++) {
			var p = $('<p>').text(String(args[i])).appendTo(message);
		}
		message.appendTo(this.widget);
		
		input.appendTo(this.widget).focus();
		
		var ok = $('<button>')
			.text('OK')
			.appendTo(this.widget)
			.click(function () {
				self.widget.empty();
				next(input.val());
			});
		
	};

	QVM.defaultInternals['TEST'] = function (args, next) {
		next(args[0]*2);
	};

	QVM.defaultInternals['CLOG'] = function (args, next) {
		console.log(args);
		next();
	};

	QVM.defaultInternals['RAND'] = function (args, next) {
		var rMin = 0, rMax, r;
		if(args.length == 0) {
			throw('RAND expects at least one arguments. '+args.length+' given');
		}
		if(args.length == 1) {
			rMax = args[0];
		} else {
			rMin = args[0];
			rMax = args[1];
		}
		r = Math.round(Math.abs(rMax - rMin) * Math.random() + rMin);
		next(r);
	};

	QVM.defaultInternals['SLEEP'] = function (args, next) {
		var interval = args[0];
		window.setTimeout(next, interval);
	};
	
	
} (jQuery) );
